package com.markus.code.exam.bianlifeng;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Stack;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/18 17:04
 * todo:
 */
class Graph {
    public HashMap<Integer, Node> nodes;
    public HashSet<Edge> edges;
    public Graph(){
        nodes = new HashMap<>();
        edges = new HashSet<>();
    }
}
class Node {
    public Integer value;
    public Integer in;
    public Integer out;
    public ArrayList<Node> nexts;
    public ArrayList<Edge> edges;
    public Node(Integer value){
        this.value = value;
        in = 0;
        out = 0;
        nexts = new ArrayList<>();
        edges = new ArrayList<>();
    }
}
class Edge {
    public Integer weight;
    public Node from;
    public Node to;
    public Edge (Integer weight, Node from, Node to){
        this.weight = weight;
        this.from = from;
        this.to = to;
    }
}
class GraphGenerator {
    public static Graph createNoGraph(int [][]matrix){
        Graph graph = new Graph();
        for (int i=0;i<matrix.length;i++){
            Integer weight = matrix[i][0];
            Integer from = matrix[i][1];
            Integer to = matrix[i][2];
            //如果不存在就创建一下
            if (!graph.nodes.containsKey(from)){
                graph.nodes.put(from,new Node(from));
            }
            if (!graph.nodes.containsKey(to)){
                graph.nodes.put(to,new Node(to));
            }
            Node fromNode = graph.nodes.get(from);
            Node toNode = graph.nodes.get(to);
            Edge edge = new Edge(weight,fromNode,toNode);
            graph.edges.add(edge);
            fromNode.nexts.add(toNode);
            toNode.nexts.add(fromNode);
            fromNode.edges.add(edge);
            toNode.edges.add(edge);
            fromNode.out++;
            toNode.out++;
            fromNode.in++;
            toNode.in++;
        }
        return graph;
    }
}
class GraphDfs {
    public void dfs(Node node){
        if (node == null){
            return ;
        }
        Stack<Node> stack = new Stack<>();
        HashSet<Node> set = new HashSet<>();
        stack.push(node);
        set.add(node);
        //入栈就打印
        System.out.print(node.value+" ");
        while(!stack.isEmpty()){
            Node pre = stack.pop();
            for (Node next : pre.nexts) {
                if (!set.contains(next)){
                    stack.push(pre);
                    stack.push(next);
                    set.add(next);
                    System.out.print(next.value+" ");
                    break;
                }
            }
        }
    }
}
public class Main {

}
